<template>
  <!-- 滚动导航 -->
  <div class="header-nav-box">
    <div ref="scroll" class="scroll-to" :style="{ left: leftValue }"></div>
    <div class="header-nav-item">
      <span @click="scroll" ref="sp0">晒图</span>
    </div>
    <div class="header-nav-item">
      <span @click="scroll" ref="sp1">详情</span>
    </div>
    <div class="header-nav-item">
      <span @click="scroll" ref="sp2">须知</span>
    </div>
    <div class="header-nav-item">
      <span @click="scroll" ref="sp3">附近</span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    index: [Number, String],
    showScrollTarBar: Boolean
  },
  data() {
    return {
      scrollEle: null
    }
  },
  computed: {
    leftValue() {
      if (!this.showScrollTarBar) return
      const ele = 'sp' + this.index
      return this.$refs[ele].offsetLeft + 'px'
    }
  },
  methods: {
    scroll(e) {
      this.scrollEle.style.left = e.target.offsetLeft + 'px'
      this.$emit('scrollTo', e.target.textContent)
    }
  },
  mounted() {
    this.scrollEle = this.$refs.scroll
  }
}
</script>

<style lang="less" scoped>
.header-nav-box {
  z-index: 20;
  left: 0;
  top: 40px;
  width: 100%;
  height: 50px;
  position: fixed;
  background-color: #fff;
  display: flex;
  justify-content: space-around;
  align-items: center;

  .header-nav-item {
    flex: 1;
    height: 100%;
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    color: #4a4a4a;
    font-weight: 700;

    span {
      padding: 0 10px;
    }
  }

  .scroll-to {
    border-radius: 999px;
    background-color: #d94648;
    position: absolute;
    bottom: 5px;
    height: 4px;
    width: 15%;
    z-index: 21;
    transition: all 0.3s linear;
  }
}
</style>
